/// @tags: DP
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define rint register int

using std::cin;
using std::cout;
using std::max;
using std::min;

namespace BlueQuantum {

constexpr int maxn = 5e2 + 3, mod = 1024523;

int n, m;
int f[2][maxn][maxn];
char a[maxn], b[maxn];

void inc(int &x, int y) {
  if ((x += y) >= mod) x -= mod;
}

inline int main() {
  cin >> n >> m >> (a + 1) >> (b + 1);
  std::reverse(a + 1, a + 1 + n);
  std::reverse(b + 1, b + 1 + m);
  f[0][0][0] = 1;
  for (int k = 1; k <= n + m; ++k) {
    int now = k & 1, pre = now ^ 1;
    memset(f[now], 0, sizeof(f[now]));
    for (int i = max(0, k - m); i <= min(n, k); ++i)
      for (int j = max(0, k - m); j <= min(n, k); ++j) {
        if (i && j && a[i] == a[j]) inc(f[now][i][j], f[pre][i - 1][j - 1]);
        if (i && k - j && a[i] == b[k - j]) inc(f[now][i][j], f[pre][i - 1][j]);
        if (j && k - i && b[k - i] == a[j]) inc(f[now][i][j], f[pre][i][j - 1]);
        if (k - i && k - j && b[k - i] == b[k - j]) inc(f[now][i][j], f[pre][i][j]);
      }
  }
  cout << f[(n + m) & 1][n][n];
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P1758 [NOI2009] 管道取珠.in", "r", stdin);
  freopen("P1758 [NOI2009] 管道取珠.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
